<?php

/**
* alltosun.com 商品控制器 index.php
* ============================================================================
* 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
* 网站地址: http://www.alltosun.com
* ----------------------------------------------------------------------------
* 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
* ============================================================================
* $Author: 吴松键 (wusj@alltosun.com) $
* $Date: 2014-8-8 下午12:36:18 $
* $Id$
*/

class Action
{
    private $per_page = 30;

    /**
     * 显示商品列表
     */
    public function index()
    {
        $page          = Request::Get('page_no', 1);
        $status        = Request::getParam('status', 1);
        $is_advenced   = Request::getParam('is_advenced', 0);
        $search_filter = Request::getParam('search_filter', array());

        // 拼装搜索条件
        $is_empty = 0;
        $filter = array();
        if (isset($search_filter['title']) && $search_filter['title']) {
            //通过快照查询order_ids
            $goods_filter['`title` LIKE '] = '%'.$search_filter['title'].'%';
            $goods_ids = _model('goods')->getFields('id', $goods_filter);
            if ($goods_ids) {
                $filter['goods_id'] = $goods_ids;
            } else {
                $is_empty = 1;
            }
        }
        
        if (isset($search_filter['user_name']) && $search_filter['user_name']) {
            //通过快照查询order_ids
            $user_filter['`user_name` LIKE '] = '%'.$search_filter['user_name'].'%';
            $user_ids = _model('user')->getFields('id', $user_filter);
            if ($user_ids) {
                $filter['user_id'] = $user_ids;
            } else {
                $is_empty = 1;
            }
        }
        $filter['status']  = $status;

        $store_goods_list = array();
        if (!$is_empty) {
            $store_goods_count = _model('store_goods')->getTotal($filter);
            if ($store_goods_count) {
                $pager = new Pager($this->per_page);
                if ($pager->generate($store_goods_count)) {
                    Response::assign('pager', $pager);
                }

                $store_goods_list = _model('store_goods')->getList($filter, ' ORDER BY `add_time` DESC '.$pager->getLimit($page));
                foreach ($store_goods_list as $key=>$value) {
                    $goods_info = _uri('goods', $value['goods_id']);
                    $store_goods_list[$key]['goods_info'] = $goods_info;
                    // 获取商品价格
                    //$value['price'] = goods_helper::get_store_price_by_id($value['id']);
                    // 获取商品库存
                    //$value['stock'] = goods_helper::get_store_stock_by_id($value['id']);
                }
            }
        }
        
        Response::assign('store_goods_list', $store_goods_list);
        Response::assign('status', $status);
        Response::assign('search_filter', $search_filter);
        Response::assign('is_advenced', $is_advenced);

        Response::display('admin/store_goods_list.html');
    }


    public function get_sku_list()
    {
        $goods_id = Request::Post('goods_id', 0);
        $store_id = Request::Post('store_id', 0);

        $store_info = _uri('store', $store_id);
        $goods_info = _uri('goods', $goods_id);

        if (!$goods_info) {
            return '请选择要查看的信息有误';
        }

        $sku_ids = _model('store_goods_sku')->getFields('sku_id', array('store_id'=>$store_id, 'goods_id'=>$goods_id, 'status'=>1));
        //$sku_ids = _model('sku_relation')->getFields('sku_id', array('sku_id'=>$sku_ids), ' ORDER BY `property_id` ASC , `property_value_id` ASC, `sku_id` ASC ');
        $sku_ids = array_unique($sku_ids);
        $sku_list = array();
        if (!empty($sku_ids[1])) {
            foreach ($sku_ids as $k=>$v) {
                $sku_info = _uri('store_goods_sku', array('store_id'=>$store_id, 'goods_id'=>$goods_id, 'sku_id'=>$v));
                $line_list = goods_helper::get_property_value_by_sku_id($v);

                $sku_value_ids = '';
                foreach ($line_list as $key=>$value) {
                    $sku_value_ids .= '-'.$value['property_value_id'];
                }

                $sku_info['line_list']     = $line_list;
                $sku_info['sku_value_ids'] = trim($sku_value_ids, '-');
                $sku_list[] = $sku_info;
            }
        }
        Response::assign('sku_list',$sku_list);
        Response::assign('goods_info', $goods_info);
        Response::display('admin/sku_info.html');
    }
}
?>